<?php

namespace App\Http\Controllers\Acl;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Repositories\Eloquent\Acl\StaffRepository as Staff;
use App\Repositories\Eloquent\Acl\RoleRepository as Role;

class StaffController extends Controller
{
    protected $staff, $role;

    public function __construct(Staff $staff, Role $role)
    {
        parent::__construct();

        $this->staff = $staff;

        $this->role = $role;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $staffs = $this->staff->index()->get();

        return view($this->view, compact('staffs'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $roles = $this->role->all();

        $branches = \App\Models\Mgt\Branch::all();

        return view($this->view, compact('roles', 'branches'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(\App\Http\Requests\StaffRequest $request)
    {
        return $this->staff->store($request);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $staff = $this->staff->show($id);

        if ($staff instanceof JsonResponse && $staff->getStatusCode() === 404) {
            return abort('404');
        }

        $roles = $this->role->all();

        $branches = \App\Models\Mgt\Branch::all();

        return view($this->view, compact('staff', 'roles', 'branches'));
    }

    public function passwd()
    {
        $staff = Auth('admin')->user();

        return view($this->view, compact('staff'));
    }


    public function chgpwd(Request $request, $id)
    {
        return $this->staff->chgpwd($request, $id);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        return $this->staff->renew($request, $id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
